package com.shaco.heroproject.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.shaco.heroproject.entity.Result_AllHero;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/11.
 */
public class MySQLiteAllHeroUtil {
    private SQLiteDatabase db;
    private Context context;

    public MySQLiteAllHeroUtil(Context c) {
        // TODO Auto-generated constructor stub
        context = c;
        init();
    }

    private void init() {
        MySQLliteOpenHelper helper = new MySQLliteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
        db = helper.getWritableDatabase();
    }

    public void saveDatasToDataBase(List<Result_AllHero> results) {
        for (Result_AllHero result : results) {
            ContentValues values = new ContentValues();
            values.put(Constant.COLUMN_HERO_ALL_ID, result.getId());
            values.put(Constant.COLUMN_HERO_ALL_IMG,result.getImg());
            values.put(Constant.COLUMN_HERO_ALL_NAME_C,result.getName_c());
            values.put(Constant.COLUMN_HERO_ALL_TAGS,result.getTags());
            values.put(Constant.COLUMN_HERO_ALL_TITLE,result.getTitle());
            db.insert(Constant.TABLE_NAME_ALLHERORESULT, null, values);
        }
    }
    public List<Result_AllHero> queryAll() {
        String sql = " SELECT * FROM " + Constant.TABLE_NAME_ALLHERORESULT;
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor != null) {
            List<Result_AllHero> results = new ArrayList<Result_AllHero>();
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HERO_ALL_ID));
                String img = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HERO_ALL_IMG));
                String name_c = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HERO_ALL_NAME_C));
                String tags = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HERO_ALL_TAGS));
                String title = cursor.getString(cursor.getColumnIndex(Constant.COLUMN_HERO_ALL_TITLE));

                Result_AllHero result = new Result_AllHero();
                result.setId(id);
                result.setImg(img);
                result.setName_c(name_c);
                result.setTags(tags);
                result.setTitle(title);
                results.add(result);
            }
            cursor.close();
            return results;
        }
        return null;
    }
    public void deleteByTitle(int id) {
        String sql = " DELETE  FROM " + Constant.TABLE_NAME_ALLHERORESULT + " WHERE id = " + id;
        db.execSQL(sql);
    }

    public void deleteAll() {
        String sql = " DELETE FROM " + Constant.TABLE_NAME_ALLHERORESULT;
        db.execSQL(sql);
    }


}
